package com.lurencun.service.autoupdate.utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;

/**
 * @author EnzoChan
 * @create 2019/7/8
 * @desc:
 */
public class SlientInstallUtil {

    //静默安装

    public static void installSlient(String filePath) {
        String cmd = "pm install -r " + filePath;
        Process process = null;
        DataOutputStream os = null;
        BufferedReader successResult = null;
        BufferedReader errorResult = null;
        StringBuilder successMsg = null;
        StringBuilder errorMsg = null;
        try {
            //静默安装需要root权限
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.write(cmd.getBytes());
            os.writeBytes("\n");
            os.writeBytes("exit\n");
            os.flush();
            //获取返回结果
            successMsg = new StringBuilder();
            errorMsg = new StringBuilder();

            //执行命令
            process.waitFor();

//            successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
//            errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
//            String s;
//            while ((s = successResult.readLine()) != null) {
//                successMsg.append(s);
//            }
//            while ((s = errorResult.readLine()) != null) {
//                errorMsg.append(s);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (process != null) {
                    process.destroy();
                }
                if (successResult != null) {
                    successResult.close();
                }
                if (errorResult != null) {
                    errorResult.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

}
